// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
	env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
	// 获取传参过来的数据
	const {UID, post_time, formData, nickname, avatarUrl, image, second_category_index, userID} = event
	// 将formData的内容进行解构
	const {item_name, description, location, contact_way} = formData
	// 此云函数是其它提交云函数，设置默认数据
	let tags, type, is_found
	if (second_category_index == 4) {
		tags = 'lost'
		type = '其它'
		is_found = false
	}else{
		tags = 'found'
		type = '其它'
		is_found = false
	}

	return await db.collection('lost_and_found_infos').add({
		data: {
			UID,
			post_time,
			nickname,
			avatarUrl,
			item_name,
			description,
			location,
			contact_way,
			tags,
			type,
			is_found,
			image: [image],
			userID
		}
	})
}
